﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.OleDb;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Model;
namespace Thep
{
    public partial class FrmLoaiThepCot : Form
    {
        public FrmLoaiThepCot()
        {
            InitializeComponent();
        }

        #region Database
        OleDbConnection conn;
        OleDbCommand cmd;

        public void Connect()
        {
            string connectstring = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + Application.StartupPath + "\\Database.a;Persist Security Info=False";
            conn = new OleDbConnection(connectstring);
            cmd = conn.CreateCommand();
        }
        #endregion

        private string loaithepID="";
        private string loaithepdamID = "";

        #region Methods

        #region Method Cot thep

        public void ActiveTextCot(bool flag)
        {
            txtBeRongBanCanhCotThep.Enabled = flag;
            txtChieuCaoDuongHanCotThep.Enabled = flag;
            txtChieuCaoTietDienCotThep.Enabled = flag;
            txtChieuDayBanBungCotThep.Enabled = flag;
            txtChieuDayBanCanhCotThep.Enabled = flag;
            txtTenCotThep.Enabled = flag;
            ckbCotThepToHop.Enabled = flag;
            ckbCotThepHinh.Enabled = flag;
            txtDienTichTietDienCotThep.Enabled = flag;
            grcCotThep.Enabled = !flag;
        }

        public void CleanTextCot()
        {
            txtBeRongBanCanhCotThep.Text = "";
            txtChieuCaoDuongHanCotThep.Text = "";
            txtChieuCaoTietDienCotThep.Text = "";
            txtChieuDayBanBungCotThep.Text = "";
            txtChieuDayBanCanhCotThep.Text = "";
            txtTenCotThep.Text = "";
            txtDienTichTietDienCotThep.Text = "";
        }

        public void ActiveButtonCot(bool them, bool luu, bool xoa, bool huy)
        {
            btnThemCotThep.Enabled = them;
            btnLuuCotThep.Enabled = luu;
            btnXoaCotThep.Enabled = xoa;
            btnhuyCotThep.Enabled = huy;
        }

        public void InsertThepCot(LoaiThepCot thep)
        {
            try
            {
                cmd.CommandText = "INSERT INTO LoaiThepCot(TenLoaiThepCot,ChieuCaoTietDien,ChieuDayBanBung,BeRongBanCanh,ChieuDayBanCanh,ChieuCaoDuongHan,ThepToHop,DienTichTietDienCotThep) VALUES('" + thep.TenLoaiThepCot + "','" + thep.ChieuCaoTietDien + "','" + thep.ChieuDayBanBung + "','" + thep.BeRongBanCanh + "','" + thep.ChieuDayBanCanh + "','" + thep.ChieuCaoDuongHan + "','" + thep.ThepToHop + "','" + thep.DienTichTietDienCotThep + "')";
                cmd.CommandType = CommandType.Text;
                conn.Open();

                cmd.ExecuteNonQuery();
            }
            catch (Exception)
            {
                throw;
            }
            finally
            {
                if (conn != null) conn.Close();
            }
        }

        public DataTable LoadThepCot() {
            Connect();
            DataTable loaicotthep = new DataTable();
            loaicotthep.Columns.Add("LoaiThepCotID", typeof(int));
            loaicotthep.Columns.Add("TenLoaiThepCot", typeof(string));
            try
            {
                cmd.CommandText = "SELECT * FROM LoaiThepCot";
                cmd.CommandType = CommandType.Text;
                conn.Open();

                OleDbDataReader reader = cmd.ExecuteReader();

                while (reader.Read())
                {
                    DataRow dr = loaicotthep.NewRow();
                    dr["LoaiThepCotID"] = int.Parse(reader["LoaiThepCotID"].ToString());
                    dr["TenLoaiThepCot"] = reader["TenLoaiThepCot"].ToString();
                    loaicotthep.Rows.Add(dr);
                }
                return loaicotthep;
            }
            catch (Exception)
            {
                throw;
            }
            finally
            {
                if (conn != null) conn.Close();
            }
        }

        public LoaiThepCot FindLoaiThepCot(int t)
        {
            try
            {
                Connect();
                LoaiThepCot thep = new LoaiThepCot();
                cmd.CommandText = "SELECT * FROM LoaiThepCot WHERE LoaiThepCotID=" + t;
                cmd.CommandType = CommandType.Text;
                conn.Open();
                OleDbDataReader reader = cmd.ExecuteReader();
                while (reader.Read())
                {
                    thep.LoaiThepCotID = int.Parse(reader["LoaiThepCotID"].ToString());
                    thep.TenLoaiThepCot = reader["TenLoaiThepCot"].ToString();
                    thep.ChieuCaoTietDien = float.Parse(reader["ChieuCaoTietDien"].ToString());
                    thep.ChieuDayBanBung = float.Parse(reader["ChieuDayBanBung"].ToString());
                    thep.BeRongBanCanh = float.Parse(reader["BeRongBanCanh"].ToString());
                    thep.ChieuDayBanCanh = float.Parse(reader["ChieuDayBanCanh"].ToString());
                    thep.ChieuCaoDuongHan = float.Parse(reader["ChieuCaoDuongHan"].ToString());
                    thep.DienTichTietDienCotThep = float.Parse(reader["DienTichTietDienCotThep"].ToString());
                    thep.ThepToHop = int.Parse(reader["ThepToHop"].ToString());
                }


                return thep;
            }
            catch (Exception)
            {
                throw;
            }
            finally
            {
                if (conn != null) conn.Close();
            }
        }

        public void DeleteLoaiThepCot(int id)
        {
            try
            {
                cmd.CommandText = "DELETE FROM LoaiThepCot WHERE LoaiThepCotID = " + id;
                cmd.CommandType = CommandType.Text;
                conn.Open();

                cmd.ExecuteNonQuery();
            }
            catch (Exception)
            {
                throw;
            }
            finally
            {
                if (conn != null) conn.Close();
            }
        }

        public void Delete(LoaiThepCot m)
        {

            try
            {
                cmd.CommandText = "DELETE FROM KetQua WHERE LoaiThepCotID = " + m.LoaiThepCotID;
                cmd.CommandType = CommandType.Text;
                conn.Open();

                cmd.ExecuteNonQuery();
            }
            catch (Exception)
            {
                throw;
            }
            finally
            {
                if (conn != null) conn.Close();
            }

        }

        public KetQua FindKetQua(LoaiThepCot m)
        {
            try
            {
                Connect();
                KetQua ketqua = new KetQua();
                cmd.CommandText = "SELECT * FROM KetQua WHERE LoaiThepCotID = " + m.LoaiThepCotID;
                cmd.CommandType = CommandType.Text;
                conn.Open();
                OleDbDataReader reader = cmd.ExecuteReader();
                while (reader.Read())
                {
                    ketqua.KetQuaID = int.Parse(reader["KetQuaID"].ToString());
                }
                return ketqua;
            }
            catch (Exception)
            {
                throw;
            }
            finally
            {
                if (conn != null) conn.Close();
            }

        }

        #endregion

        #region Method Dam thep

        public void ActiveTextDam(bool flag)
        {
            txtBeRongBanCanhDamThep.Enabled = flag;
            txtChieuCaoDuongHanDamThep.Enabled = flag;
            txtChieuCaoTietDienDamThep.Enabled = flag;
            txtChieuDayBanBungDamThep.Enabled = flag;
            txtChieuDayBanCanhDamThep.Enabled = flag;
            txtTenDamThep.Enabled = flag;
            ckbDamThepTohop.Enabled = flag;
            ckbDamThepHinh.Enabled = flag;
            grcDamThep.Enabled = !flag;
            
        }

        public void CleanTextDam()
        {
            txtBeRongBanCanhDamThep.Text = "";
            txtChieuCaoDuongHanDamThep.Text = "";
            txtChieuCaoTietDienDamThep.Text = "";
            txtChieuDayBanBungDamThep.Text = "";
            txtChieuDayBanCanhDamThep.Text = "";
            txtTenDamThep.Text = "";
        }

        public void ActiveButtonDam(bool them, bool luu, bool xoa, bool huy)
        {
            btnThemDamThep.Enabled = them;
            btnLuuDamThep.Enabled = luu;
            btnXoaDamThep.Enabled = xoa;
            btnHuyDamThep.Enabled = huy;
        }

        public void InsertThepDam(LoaiThepDam thep)
        {
            try
            {
                cmd.CommandText = "INSERT INTO LoaiThepDam(TenLoaiThepDam,ChieuCaoTietDien,ChieuDayBanBung,BeRongBanCanh,ChieuDayBanCanh,ChieuCaoDuongHan,ThepToHop) VALUES('" + thep.TenLoaiThepDam + "','" + thep.ChieuCaoTietDien + "','" + thep.ChieuDayBanBung + "','" + thep.BeRongBanCanh + "','" + thep.ChieuDayBanCanh + "','" + thep.ChieuCaoDuongHan + "','" + thep.ThepToHop + "')";
                cmd.CommandType = CommandType.Text;
                conn.Open();

                cmd.ExecuteNonQuery();
            }
            catch (Exception)
            {
                throw;
            }
            finally
            {
                if (conn != null) conn.Close();
            }
        }

        public DataTable LoadThepDam()
        {
            Connect();
            DataTable loaidamthep = new DataTable();
            loaidamthep.Columns.Add("LoaiThepDamID", typeof(int));
            loaidamthep.Columns.Add("TenLoaiThepDam", typeof(string));
            try
            {
                cmd.CommandText = "SELECT * FROM LoaiThepDam";
                cmd.CommandType = CommandType.Text;
                conn.Open();

                OleDbDataReader reader = cmd.ExecuteReader();

                while (reader.Read())
                {
                    DataRow dr = loaidamthep.NewRow();
                    dr["LoaiThepDamID"] = int.Parse(reader["LoaiThepDamID"].ToString());
                    dr["TenLoaiThepDam"] = reader["TenLoaiThepDam"].ToString();
                    loaidamthep.Rows.Add(dr);
                }
                return loaidamthep;
            }
            catch (Exception)
            {
                throw;
            }
            finally
            {
                if (conn != null) conn.Close();
            }
        }

        public LoaiThepDam FindLoaiThepDam(int t)
        {
            try
            {
                Connect();
                LoaiThepDam thep = new LoaiThepDam();
                cmd.CommandText = "SELECT * FROM LoaiThepDam WHERE LoaiThepDamID=" + t;
                cmd.CommandType = CommandType.Text;
                conn.Open();
                OleDbDataReader reader = cmd.ExecuteReader();
                while (reader.Read())
                {
                    thep.LoaiThepDamID = int.Parse(reader["LoaiThepDamID"].ToString());
                    thep.TenLoaiThepDam = reader["TenLoaiThepDam"].ToString();
                    thep.ChieuCaoTietDien = float.Parse(reader["ChieuCaoTietDien"].ToString());
                    thep.ChieuDayBanBung = float.Parse(reader["ChieuDayBanBung"].ToString());
                    thep.BeRongBanCanh = float.Parse(reader["BeRongBanCanh"].ToString());
                    thep.ChieuDayBanCanh = float.Parse(reader["ChieuDayBanCanh"].ToString());
                    thep.ChieuCaoDuongHan = float.Parse(reader["ChieuCaoDuongHan"].ToString());
                    thep.ThepToHop = int.Parse(reader["ThepToHop"].ToString());
                }


                return thep;
            }
            catch (Exception)
            {
                throw;
            }
            finally
            {
                if (conn != null) conn.Close();
            }
        }

        public void DeleteLoaiThepDam(int id)
        {
            try
            {
                cmd.CommandText = "DELETE FROM LoaiThepDam WHERE LoaiThepDamID = " + id;
                cmd.CommandType = CommandType.Text;
                conn.Open();

                cmd.ExecuteNonQuery();
            }
            catch (Exception)
            {
                throw;
            }
            finally
            {
                if (conn != null) conn.Close();
            }
        }

        public void Delete(LoaiThepDam m)
        {

            try
            {
                cmd.CommandText = "DELETE FROM KetQua WHERE LoaiThepDamID = " + m.LoaiThepDamID;
                cmd.CommandType = CommandType.Text;
                conn.Open();

                cmd.ExecuteNonQuery();
            }
            catch (Exception)
            {
                throw;
            }
            finally
            {
                if (conn != null) conn.Close();
            }

        }

        public KetQua FindKetQua(LoaiThepDam m)
        {
            try
            {
                Connect();
                KetQua ketqua = new KetQua();
                cmd.CommandText = "SELECT * FROM KetQua WHERE LoaiThepDamID = " + m.LoaiThepDamID;
                cmd.CommandType = CommandType.Text;
                conn.Open();
                OleDbDataReader reader = cmd.ExecuteReader();
                while (reader.Read())
                {
                    ketqua.KetQuaID = int.Parse(reader["KetQuaID"].ToString());
                }
                return ketqua;
            }
            catch (Exception)
            {
                throw;
            }
            finally
            {
                if (conn != null) conn.Close();
            }

        }

        #endregion

        #endregion

        #region Form Event

        #region Cot Thep Panel

        private void cbCotThepToHop_CheckedChanged(object sender, EventArgs e)
        {
            ckbCotThepHinh.CheckState = ckbCotThepToHop.CheckState == CheckState.Checked ? CheckState.Unchecked : CheckState.Checked;
            ckbCotThepHinh.Checked = ckbCotThepToHop.Checked == true ? false : true;

            if (ckbCotThepToHop.Checked == true)
            {
                lbCotThepDuongHan.Text = "Bán kính góc bụng - cánh cột";
                lbCotThepDuongHan.Location = new Point
                {
                    X = lbCotThepDuongHan.Left-36,
                    Y = lbCotThepDuongHan.Top
                };
            }
            else
            {
                lbCotThepDuongHan.Text = "Chiều cao đường hàn";
                lbCotThepDuongHan.Location = new Point
                {
                    X = lbCotThepDuongHan.Left+36,
                    Y = lbCotThepDuongHan.Top
                };
            }
        }

        private void btnThemCotThep_Click(object sender, EventArgs e)
        {
            Connect();
            CleanTextCot();
            grcCotThep.Enabled = false;
            ActiveButtonCot(false, true, false, true);
            ActiveTextCot(true);
            ckbCotThepToHop.Checked = false;
            txtTenCotThep.Focus();
        }
        
        private void btnLuuCotThep_Click(object sender, EventArgs e)
        {
            Connect();

            if (txtTenCotThep.Text == "") { txtTenCotThep.ErrorText = "Nhập tên loại thép"; return; }
            if (txtBeRongBanCanhCotThep.Text == "") { txtBeRongBanCanhCotThep.ErrorText = "Nhập bề rộng bản cánh"; return; }
            if (txtChieuCaoTietDienCotThep.Text == "") { txtChieuCaoTietDienCotThep.ErrorText = "Nhập chiều cao tiết diện"; return; }
            if (txtChieuDayBanBungCotThep.Text == "") { txtChieuDayBanBungCotThep.ErrorText = "Nhập chiều dày bản bụng"; return; }
            if (txtDienTichTietDienCotThep.Text == "") { txtDienTichTietDienCotThep.ErrorText = "Nhập diện tích tiết diện cột thép"; return; }
            if (txtChieuDayBanCanhCotThep.Text == "") { txtChieuDayBanCanhCotThep.ErrorText = "Nhập chiều dày bản cánh"; return; }
            if (txtChieuCaoDuongHanCotThep.Text == "") { txtChieuCaoDuongHanCotThep.ErrorText = "Không được để trống"; return; }

            LoaiThepCot thep = new LoaiThepCot();
            thep.TenLoaiThepCot = txtTenCotThep.Text.Trim();
            thep.ChieuCaoTietDien = float.Parse(txtChieuCaoTietDienCotThep.Text.Trim());
            thep.ChieuDayBanBung = float.Parse(txtChieuDayBanBungCotThep.Text.Trim());
            thep.BeRongBanCanh = float.Parse(txtBeRongBanCanhCotThep.Text.Trim());
            thep.ChieuDayBanCanh = float.Parse(txtChieuDayBanCanhCotThep.Text.Trim());
            thep.ChieuCaoDuongHan = float.Parse(txtChieuCaoDuongHanCotThep.Text.Trim());
            if (ckbCotThepToHop.Checked == false) thep.ThepToHop = 0; else thep.ThepToHop = 1;
            thep.DienTichTietDienCotThep = float.Parse(txtDienTichTietDienCotThep.Text.Trim());
            InsertThepCot(thep);

            CleanTextCot();
            ActiveButtonCot(true, false, false, false);
            grcCotThep.Enabled = true;
            ActiveTextCot(false);
            ckbCotThepToHop.Checked = false;
            grcCotThep.DataSource = LoadThepCot();
        }

        private void grvCotThep_RowClick(object sender, DevExpress.XtraGrid.Views.Grid.RowClickEventArgs e)
        {
            Connect();
            if (grvCotThep.SelectedRowsCount > 0)
            {
                LoaiThepCot thep = FindLoaiThepCot((int)grvCotThep.GetRowCellValue(grvCotThep.GetSelectedRows()[0], "LoaiThepCotID"));
                if (thep != null)
                {
                    loaithepID = thep.LoaiThepCotID.ToString();
                    txtTenCotThep.Text = thep.TenLoaiThepCot;
                    txtBeRongBanCanhCotThep.Text = thep.BeRongBanCanh.ToString();
                    txtChieuCaoDuongHanCotThep.Text = thep.ChieuCaoDuongHan.ToString();
                    txtChieuCaoTietDienCotThep.Text = thep.ChieuCaoTietDien.ToString();
                    txtChieuDayBanBungCotThep.Text = thep.ChieuDayBanBung.ToString();
                    txtChieuDayBanCanhCotThep.Text = thep.ChieuDayBanCanh.ToString();
                    txtDienTichTietDienCotThep.Text = thep.DienTichTietDienCotThep.ToString();
                    ckbCotThepToHop.Checked = thep.ThepToHop == 0 ? false : true;

                    ActiveButtonCot(true, false, true, true);
                }
            }
        }

        private void btnXoaCotThep_Click(object sender, EventArgs e)
        {
            Connect();
            if (loaithepID != "") {
                LoaiThepCot thep = FindLoaiThepCot(int.Parse(loaithepID));
                if (thep != null)
                {
                    KetQua k = FindKetQua(thep);
                    if (k.KetQuaID != 0)
                    {
                        DialogResult rs = MessageBox.Show("Thông số này đã được dùng để tính toán, nếu bạn xóa thông số này, chương trình sẽ xóa cả kết quả dùng thông số này để tính toán!! Bạn có chắc muốn xóa??", "Xác nhận", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
                        if (rs == DialogResult.Yes)
                        {
                            DeleteLoaiThepCot(thep.LoaiThepCotID);
                            Delete(thep);
                        }
                    }
                    else { DeleteLoaiThepCot(int.Parse(loaithepID)); }
                    CleanTextCot();
                    ActiveButtonCot(true, false, false, false);
                    grcCotThep.DataSource = LoadThepCot();
                }
            }
        }

        private void btnhuyCotThep_Click(object sender, EventArgs e)
        {
            Connect();
            loaithepID = "";
            CleanTextCot();
            ActiveButtonCot(true,false,false,false);
            ActiveTextCot(false);
            ckbCotThepToHop.Checked = false;
            grcCotThep.DataSource = LoadThepCot();
        }

        private void ckbCotThepHinh_CheckedChanged(object sender, EventArgs e)
        {
            ckbCotThepToHop.CheckState = ckbCotThepHinh.CheckState == CheckState.Checked ? CheckState.Unchecked : CheckState.Checked;
            ckbCotThepToHop.Checked = ckbCotThepHinh.Checked == true ? false : true;
        }

        #endregion

        #region Dam Thep Panel

        private void cbDamThepToHop_CheckedChanged(object sender, EventArgs e)
        {
            ckbDamThepHinh.CheckState = ckbDamThepTohop.CheckState == CheckState.Checked ? CheckState.Unchecked : CheckState.Checked;
            ckbDamThepHinh.Checked = ckbDamThepTohop.Checked == true ? false : true;

            if (ckbDamThepTohop.Checked == true)
            {
                lbDamThepDuongHan.Text = "Bán kính góc bụng - cánh cột";
                lbDamThepDuongHan.Location = new Point
                {
                    X = lbDamThepDuongHan.Left - 36,
                    Y = lbDamThepDuongHan.Top
                };
            }
            else
            {
                lbDamThepDuongHan.Text = "Chiều cao đường hàn";
                lbDamThepDuongHan.Location = new Point
                {
                    X = lbDamThepDuongHan.Left + 36,
                    Y = lbDamThepDuongHan.Top
                };
            }
        }

        private void btnThemDamThep_Click(object sender, EventArgs e)
        {
            Connect();
            CleanTextDam();
            grcDamThep.Enabled = false;
            ActiveButtonDam(false, true, false, true);
            ActiveTextDam(true);
            ckbDamThepTohop.Checked = false;
            txtTenDamThep.Focus();
        }

        private void btnLuuDamThep_Click(object sender, EventArgs e)
        {
            Connect();

            if (txtTenDamThep.Text == "") { txtTenDamThep.ErrorText = "Nhập tên loại thép"; return; }
            if (txtBeRongBanCanhDamThep.Text == "") { txtBeRongBanCanhDamThep.ErrorText = "Nhập bề rộng bản cánh"; return; }
            if (txtChieuCaoTietDienDamThep.Text == "") { txtChieuCaoTietDienDamThep.ErrorText = "Nhập chiều cao tiết diện"; return; }
            if (txtChieuDayBanBungDamThep.Text == "") { txtChieuDayBanBungDamThep.ErrorText = "Nhập chiều dày bản bụng"; return; }
            if (txtChieuDayBanCanhDamThep.Text == "") { txtChieuDayBanCanhDamThep.ErrorText = "Nhập chiều dày bản cánh"; return; }
            if (txtChieuCaoDuongHanDamThep.Text == "") { txtChieuCaoDuongHanDamThep.ErrorText = "Không được để trống"; return; }

            LoaiThepDam thep = new LoaiThepDam();
            thep.TenLoaiThepDam = txtTenDamThep.Text.Trim();
            thep.ChieuCaoTietDien = float.Parse(txtChieuCaoTietDienDamThep.Text.Trim());
            thep.ChieuDayBanBung = float.Parse(txtChieuDayBanBungDamThep.Text.Trim());
            thep.BeRongBanCanh = float.Parse(txtBeRongBanCanhDamThep.Text.Trim());
            thep.ChieuDayBanCanh = float.Parse(txtChieuDayBanCanhDamThep.Text.Trim());
            thep.ChieuCaoDuongHan = float.Parse(txtChieuCaoDuongHanDamThep.Text.Trim());
            if (ckbDamThepTohop.Checked == false) thep.ThepToHop = 0; else thep.ThepToHop = 1;
            InsertThepDam(thep);

            CleanTextDam();
            ActiveButtonDam(true, false, false, false);
            grcDamThep.Enabled = true;
            ActiveTextDam(false);
            ckbDamThepTohop.Checked = false;
            grcDamThep.DataSource = LoadThepDam();
        }

        private void grvDamThep_RowClick(object sender, DevExpress.XtraGrid.Views.Grid.RowClickEventArgs e)
        {
            Connect();
            if (grvDamThep.SelectedRowsCount > 0)
            {
                LoaiThepDam thep = FindLoaiThepDam((int)grvDamThep.GetRowCellValue(grvDamThep.GetSelectedRows()[0], "LoaiThepDamID"));
                if (thep != null)
                {
                    loaithepdamID = thep.LoaiThepDamID.ToString();
                    txtTenDamThep.Text = thep.TenLoaiThepDam;
                    txtBeRongBanCanhDamThep.Text = thep.BeRongBanCanh.ToString();
                    txtChieuCaoDuongHanDamThep.Text = thep.ChieuCaoDuongHan.ToString();
                    txtChieuCaoTietDienDamThep.Text = thep.ChieuCaoTietDien.ToString();
                    txtChieuDayBanBungDamThep.Text = thep.ChieuDayBanBung.ToString();
                    txtChieuDayBanCanhDamThep.Text = thep.ChieuDayBanCanh.ToString();
                    ckbDamThepTohop.Checked = thep.ThepToHop == 0 ? false : true;

                    ActiveButtonDam(true, false, true, true);
                }
            }
        }

        private void btnXoaDamThep_Click(object sender, EventArgs e)
        {
            if (loaithepID != "")
            {
                LoaiThepDam thep = FindLoaiThepDam(int.Parse(loaithepdamID));
                if (thep != null)
                {
                    KetQua k = FindKetQua(thep);
                    if (k.KetQuaID != 0)
                    {
                        DialogResult rs = MessageBox.Show("Thông số này đã được dùng để tính toán, nếu bạn xóa thông số này, chương trình sẽ xóa cả kết quả dùng thông số này để tính toán!! Bạn có chắc muốn xóa??", "Xác nhận", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
                        if (rs == DialogResult.Yes)
                        {
                            DeleteLoaiThepDam(int.Parse(loaithepdamID));
                            Delete(thep);
                        }
                    }
                    else { DeleteLoaiThepDam(int.Parse(loaithepdamID)); }

                    CleanTextDam();
                    ActiveButtonDam(true, false, false, false);
                    grcDamThep.DataSource = LoadThepDam();
                }
            }
        }

        private void btnhuyDamThep_Click(object sender, EventArgs e)
        {
            Connect();
            loaithepID = "";
            CleanTextDam();
            ActiveButtonDam(true, false, false, false);
            ActiveTextDam(false);
            ckbDamThepTohop.Checked = false;
            grcDamThep.DataSource = LoadThepDam();
        }

        #endregion

        private void FrmLoaiThepCot_Load(object sender, EventArgs e)
        {
            ActiveButtonCot(true,false,false,false);
            ActiveButtonDam(true,false,false,false);
            ActiveTextCot(false);
            ActiveTextDam(false);
            ckbCotThepHinh.Checked = true;
            ckbDamThepHinh.Checked = true;

            grcCotThep.DataSource = LoadThepCot();
            grcDamThep.DataSource = LoadThepDam();
        }

        #endregion

        #region Dien tich tiet dien

        private void txtChieuCaoTietDienCotThep_EditValueChanged(object sender, EventArgs e)
        {
            //=(G32*G33*2)+(G30-2*G33)*G31
            float berongcanhcot = txtBeRongBanCanhCotThep.Text != "" ? float.Parse(txtBeRongBanCanhCotThep.Text):0;
            float chieudaybancanh = txtChieuDayBanCanhCotThep.Text != "" ? float.Parse(txtChieuDayBanCanhCotThep.Text) : 0;
            float chieucaotietdien = txtChieuCaoTietDienCotThep.Text != "" ? float.Parse(txtChieuCaoTietDienCotThep.Text) : 0;
            float chieudaybanbung = txtChieuDayBanBungCotThep.Text != "" ? float.Parse(txtChieuDayBanBungCotThep.Text) : 0;
            float dientich = (berongcanhcot * chieudaybancanh * 2) + (chieucaotietdien - 2 * chieudaybancanh) * chieudaybanbung;
            txtDienTichTietDienCotThep.Text = dientich.ToString();
        }

        private void txtChieuCaoTietDienCotThep_EditValueChanging(object sender, DevExpress.XtraEditors.Controls.ChangingEventArgs e)
        {
            float berongcanhcot = txtBeRongBanCanhCotThep.Text != "" ? float.Parse(txtBeRongBanCanhCotThep.Text) : 0;
            float chieudaybancanh = txtChieuDayBanCanhCotThep.Text != "" ? float.Parse(txtChieuDayBanCanhCotThep.Text) : 0;
            float chieucaotietdien = txtChieuCaoTietDienCotThep.Text != "" ? float.Parse(txtChieuCaoTietDienCotThep.Text) : 0;
            float chieudaybanbung = txtChieuDayBanBungCotThep.Text != "" ? float.Parse(txtChieuDayBanBungCotThep.Text) : 0;
            float dientich = (berongcanhcot * chieudaybancanh * 2) + (chieucaotietdien - 2 * chieudaybancanh) * chieudaybanbung;
            txtDienTichTietDienCotThep.Text = dientich.ToString();
        }

        private void txtChieuDayBanBungCotThep_EditValueChanged(object sender, EventArgs e)
        {
            float berongcanhcot = txtBeRongBanCanhCotThep.Text != "" ? float.Parse(txtBeRongBanCanhCotThep.Text) : 0;
            float chieudaybancanh = txtChieuDayBanCanhCotThep.Text != "" ? float.Parse(txtChieuDayBanCanhCotThep.Text) : 0;
            float chieucaotietdien = txtChieuCaoTietDienCotThep.Text != "" ? float.Parse(txtChieuCaoTietDienCotThep.Text) : 0;
            float chieudaybanbung = txtChieuDayBanBungCotThep.Text != "" ? float.Parse(txtChieuDayBanBungCotThep.Text) : 0;
            float dientich = (berongcanhcot * chieudaybancanh * 2) + (chieucaotietdien - 2 * chieudaybancanh) * chieudaybanbung;
            txtDienTichTietDienCotThep.Text = dientich.ToString();
        }

        private void txtChieuDayBanBungCotThep_EditValueChanging(object sender, DevExpress.XtraEditors.Controls.ChangingEventArgs e)
        {
            float berongcanhcot = txtBeRongBanCanhCotThep.Text != "" ? float.Parse(txtBeRongBanCanhCotThep.Text) : 0;
            float chieudaybancanh = txtChieuDayBanCanhCotThep.Text != "" ? float.Parse(txtChieuDayBanCanhCotThep.Text) : 0;
            float chieucaotietdien = txtChieuCaoTietDienCotThep.Text != "" ? float.Parse(txtChieuCaoTietDienCotThep.Text) : 0;
            float chieudaybanbung = txtChieuDayBanBungCotThep.Text != "" ? float.Parse(txtChieuDayBanBungCotThep.Text) : 0;
            float dientich = (berongcanhcot * chieudaybancanh * 2) + (chieucaotietdien - 2 * chieudaybancanh) * chieudaybanbung;
            txtDienTichTietDienCotThep.Text = dientich.ToString();
        }

        private void txtBeRongBanCanhCotThep_EditValueChanged(object sender, EventArgs e)
        {
            float berongcanhcot = txtBeRongBanCanhCotThep.Text != "" ? float.Parse(txtBeRongBanCanhCotThep.Text) : 0;
            float chieudaybancanh = txtChieuDayBanCanhCotThep.Text != "" ? float.Parse(txtChieuDayBanCanhCotThep.Text) : 0;
            float chieucaotietdien = txtChieuCaoTietDienCotThep.Text != "" ? float.Parse(txtChieuCaoTietDienCotThep.Text) : 0;
            float chieudaybanbung = txtChieuDayBanBungCotThep.Text != "" ? float.Parse(txtChieuDayBanBungCotThep.Text) : 0;
            float dientich = (berongcanhcot * chieudaybancanh * 2) + (chieucaotietdien - 2 * chieudaybancanh) * chieudaybanbung;
            txtDienTichTietDienCotThep.Text = dientich.ToString();
        }

        private void txtBeRongBanCanhCotThep_EditValueChanging(object sender, DevExpress.XtraEditors.Controls.ChangingEventArgs e)
        {
            float berongcanhcot = txtBeRongBanCanhCotThep.Text != "" ? float.Parse(txtBeRongBanCanhCotThep.Text) : 0;
            float chieudaybancanh = txtChieuDayBanCanhCotThep.Text != "" ? float.Parse(txtChieuDayBanCanhCotThep.Text) : 0;
            float chieucaotietdien = txtChieuCaoTietDienCotThep.Text != "" ? float.Parse(txtChieuCaoTietDienCotThep.Text) : 0;
            float chieudaybanbung = txtChieuDayBanBungCotThep.Text != "" ? float.Parse(txtChieuDayBanBungCotThep.Text) : 0;
            float dientich = (berongcanhcot * chieudaybancanh * 2) + (chieucaotietdien - 2 * chieudaybancanh) * chieudaybanbung;
            txtDienTichTietDienCotThep.Text = dientich.ToString();
        }

        private void txtChieuDayBanCanhCotThep_EditValueChanged(object sender, EventArgs e)
        {
            float berongcanhcot = txtBeRongBanCanhCotThep.Text != "" ? float.Parse(txtBeRongBanCanhCotThep.Text) : 0;
            float chieudaybancanh = txtChieuDayBanCanhCotThep.Text != "" ? float.Parse(txtChieuDayBanCanhCotThep.Text) : 0;
            float chieucaotietdien = txtChieuCaoTietDienCotThep.Text != "" ? float.Parse(txtChieuCaoTietDienCotThep.Text) : 0;
            float chieudaybanbung = txtChieuDayBanBungCotThep.Text != "" ? float.Parse(txtChieuDayBanBungCotThep.Text) : 0;
            float dientich = (berongcanhcot * chieudaybancanh * 2) + (chieucaotietdien - 2 * chieudaybancanh) * chieudaybanbung;
            txtDienTichTietDienCotThep.Text = dientich.ToString();
        }

        private void txtChieuDayBanCanhCotThep_EditValueChanging(object sender, DevExpress.XtraEditors.Controls.ChangingEventArgs e)
        {
            float berongcanhcot = txtBeRongBanCanhCotThep.Text != "" ? float.Parse(txtBeRongBanCanhCotThep.Text) : 0;
            float chieudaybancanh = txtChieuDayBanCanhCotThep.Text != "" ? float.Parse(txtChieuDayBanCanhCotThep.Text) : 0;
            float chieucaotietdien = txtChieuCaoTietDienCotThep.Text != "" ? float.Parse(txtChieuCaoTietDienCotThep.Text) : 0;
            float chieudaybanbung = txtChieuDayBanBungCotThep.Text != "" ? float.Parse(txtChieuDayBanBungCotThep.Text) : 0;
            float dientich = (berongcanhcot * chieudaybancanh * 2) + (chieucaotietdien - 2 * chieudaybancanh) * chieudaybanbung;
            txtDienTichTietDienCotThep.Text = dientich.ToString();
        }

        private void txtChieuCaoDuongHanCotThep_EditValueChanged(object sender, EventArgs e)
        {
            float berongcanhcot = txtBeRongBanCanhCotThep.Text != "" ? float.Parse(txtBeRongBanCanhCotThep.Text) : 0;
            float chieudaybancanh = txtChieuDayBanCanhCotThep.Text != "" ? float.Parse(txtChieuDayBanCanhCotThep.Text) : 0;
            float chieucaotietdien = txtChieuCaoTietDienCotThep.Text != "" ? float.Parse(txtChieuCaoTietDienCotThep.Text) : 0;
            float chieudaybanbung = txtChieuDayBanBungCotThep.Text != "" ? float.Parse(txtChieuDayBanBungCotThep.Text) : 0;
            float dientich = (berongcanhcot * chieudaybancanh * 2) + (chieucaotietdien - 2 * chieudaybancanh) * chieudaybanbung;
            txtDienTichTietDienCotThep.Text = dientich.ToString();
        }

        private void txtChieuCaoDuongHanCotThep_EditValueChanging(object sender, DevExpress.XtraEditors.Controls.ChangingEventArgs e)
        {
            float berongcanhcot = txtBeRongBanCanhCotThep.Text != "" ? float.Parse(txtBeRongBanCanhCotThep.Text) : 0;
            float chieudaybancanh = txtChieuDayBanCanhCotThep.Text != "" ? float.Parse(txtChieuDayBanCanhCotThep.Text) : 0;
            float chieucaotietdien = txtChieuCaoTietDienCotThep.Text != "" ? float.Parse(txtChieuCaoTietDienCotThep.Text) : 0;
            float chieudaybanbung = txtChieuDayBanBungCotThep.Text != "" ? float.Parse(txtChieuDayBanBungCotThep.Text) : 0;
            float dientich = (berongcanhcot * chieudaybancanh * 2) + (chieucaotietdien - 2 * chieudaybancanh) * chieudaybanbung;
            txtDienTichTietDienCotThep.Text = dientich.ToString();
        }

        #endregion

        private void ckbDamThepHinh_CheckedChanged(object sender, EventArgs e)
        {
            ckbDamThepTohop.CheckState = ckbDamThepHinh.CheckState == CheckState.Checked ? CheckState.Unchecked : CheckState.Checked;
            ckbDamThepTohop.Checked = ckbDamThepHinh.Checked == true ? false : true;
        }



        



    }
}
